﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using VQWEB.administrator.sources;
using System.Web.Script.Serialization;
using VQWEB.common;
using VQData;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for ScheduleHandler
    /// </summary>
    public class eng_ScheduleHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Schedule
            string add_schedule = context.Request["add_schedule"];
            if (!string.IsNullOrEmpty(add_schedule))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                var obj = bussiness.InsertOrUpdate<eng_schedule>(add_schedule);
                context.Response.Write("Success Full");
            }
            string view_schedule = context.Request["view_schedule"];
            if (!string.IsNullOrEmpty(view_schedule))
            {
                string Id = context.Request["Id"];
                RenderSingleSchedule(context, Convert.ToInt32(Id));
            }
            string list_schedule = context.Request["list_schedule"];
            if (!string.IsNullOrEmpty(list_schedule))
            {
                RenderSchedule(context);
            }
            string delete_schedule = context.Request["delete_schedule"];
            if (!string.IsNullOrEmpty(delete_schedule))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.Delete<eng_schedule>(delete_schedule);
                RenderSchedule(context);
            }
            string lockunlock_schedule = context.Request["lockunlock_schedule"];
            if (!string.IsNullOrEmpty(lockunlock_schedule))
            {
                BusinessLayer<EnglishModelDataContext> bussiness = new BusinessLayer<EnglishModelDataContext>();
                bussiness.LockUnLock<eng_schedule>(lockunlock_schedule, "schedule_active");
                RenderSchedule(context);
            }
            #endregion
        }
        private void RenderSingleSchedule(HttpContext context, int Id)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            object obj = layer.Get<eng_schedule>(
               c => c.schedule_id.Equals(Id),
               c => new
               {
                   id = c.schedule_id,
                   title = c.schedule_title,
                   content = c.schedule_content,
                   active = c.schedule_active
               });
            string json = serializer.Serialize(obj);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderSchedule(HttpContext context)
        {
            DataLayer<EnglishModelDataContext> layer = new DataLayer<EnglishModelDataContext>();
            IList<object> list = layer.List<eng_schedule>(
                null,
                null,
                null,
                c => c.schedule_title,
                c => new
                {
                    id = c.schedule_id,
                    title = c.schedule_title,
                    content = c.schedule_content,
                    active = c.schedule_active
                });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}